package com.lonwin.mes2u8.domain.u8;

import lombok.Data;
import lombok.ToString;
import org.apache.commons.lang.StringUtils;

import java.time.LocalDateTime;
import java.util.Objects;

@Data
@ToString
public class DefineSyncResponse {

    private String code;
    private MyObjTypeEnum type;

    private String tradeid;
    private String ping_after;
    private String url;
    private LocalDateTime offTime;

    private boolean success;
    private String message;

    public static DefineSyncResponse buildSelfBySyncResponse(U8CommonSyncResponse response, String code, MyObjTypeEnum objType) {
        DefineSyncResponse defineSyncResponse = new DefineSyncResponse();
        defineSyncResponse.setCode(code);
        defineSyncResponse.setType(objType);
        defineSyncResponse.setTradeid(Objects.nonNull(response) ? response.getTradeid() : "");
        defineSyncResponse.setPing_after(Objects.nonNull(response) ? response.getPing_after() : "3");
        defineSyncResponse.setUrl(Objects.nonNull(response) ? response.getUrl() : "");
        defineSyncResponse.setOffTime(LocalDateTime.now().minusMinutes(Long.parseLong(StringUtils.isNotBlank(response.getPing_after()) ? response.getPing_after() : "3")));

        return defineSyncResponse;
    }
}
